const loginDao = require("../dao/login");

const { createUserJwtToken } = require("../util/server_jwt");

const loginService = {
    async register(username, password) {
        let result = await loginDao.register(username, password);
        if (result.success) {
            return { success: true, msg: '注册成功' };
        } else {
            return result;
        }
    },
    async login(username, password) {
        let result = await loginDao.login(username, password);
        if (result.success) {
            let uid = result.data.uid;
            // jwt 令牌 用于后续登录的时候 进行用户认证
            let jwt = await createUserJwtToken({ uid }, { expiresIn: '168h' });
            return { success: true, msg: '登录成功', data: { token: jwt } };
        } else {
            return result;
        }
    }
};

module.exports = loginService;